package com.vacomall.erp.web;

import com.vacomall.erp.model.Images;
import com.vacomall.erp.service.IQuarantineCertificateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by root1 on 16/8/25.
 */
@Controller
@RequestMapping(value = "downloadAction")
public class DownloadAction {

    final static Logger log = LoggerFactory.getLogger(DownloadAction.class);

    @Autowired
    private IQuarantineCertificateService quarantineCertificateService;

    /**
     * 图片下载显示
     * @param request
     * @param response
     * @param imageId
     *          图片的编号
     * @return
     */
    @RequestMapping(value = "/getImage")
    public ModelAndView get(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "imageId" , required = true ,defaultValue="") String imageId ,
                            @RequestParam(value = "imgSize" , required = true , defaultValue = "0")  int imgSize) {
        log.debug("显示图片{}", imageId);
        FileInputStream fis = null;
        OutputStream outStream = null;
        try {
            outStream = response.getOutputStream();
            Images certificateNum = quarantineCertificateService.selectByImage(imageId);

            if (certificateNum == null) {
                String message = "文件编号错误：" + imageId;
                outStream.write(message.getBytes());
                return null;
            }
            String image = "";
            if (imgSize>1){
                image =  certificateNum.getPictureUrl();
            }else {
                image = certificateNum.getThumbnailUrl();
            }
            File file = new File(image);

            if (!file.exists()) {
                log.error("文件不存在：{}", image);
                String message = "文件不存在：" + image;
                outStream.write(message.getBytes());
                return null;
            }
            //  String type = getType(imageFile.getFromat());
            //  response.setContentType(type);
            //   response.setHeader("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + "." + certificateNum.getFromat());
            fis = new FileInputStream(image);
            byte[] b = new byte[1024];
            while (fis.read(b) != -1) {
                outStream.write(b);
            }
            outStream.flush();
        } catch (IOException e) {
            log.error("下载文件出现异常", e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
